<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>OnlineBookstore后台管理系统</title>
    <link rel="stylesheet" href="/layui/css/layui.css">
    <style>
        body {
            background-image: url("/img/300.jpg");
            background-repeat: no-repeat;
            background-size: cover;
            background-attachment: fixed;
            background-position: center;
        }

        h1 {
            text-align: center;
            margin: 15px;
        }

        form {
            margin: 10%;
        }

        .demo-login-container {
            width: 320px;
            margin: 21px auto 0;
        }

        .demo-login-other .layui-icon {
            position: relative;
            display: inline-block;
            margin: 0 2px;
            top: 2px;
            font-size: 26px;
        }
    </style>
</head>
<body>
<form class="layui-form">
    <div class="demo-login-container">
        <div class="layui-form-item">
            <h1>管理员登录</h1>
            <div class="layui-input-wrap">
                <div class="layui-input-prefix">
                    <i class="layui-icon layui-icon-username"></i>
                </div>
                <input type="text" name="username" value="" lay-verify="required" placeholder="用户名"
                       lay-reqtext="请填写用户名" autocomplete="off" class="layui-input" lay-affix="clear">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-wrap">
                <div class="layui-input-prefix">
                    <i class="layui-icon layui-icon-password"></i>
                </div>
                <input type="password" name="password" value="" lay-verify="required" placeholder="密   码"
                       lay-reqtext="请填写密码" autocomplete="off" class="layui-input" lay-affix="eye">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-row">
                <div class="layui-col-xs7">
                    <div class="layui-input-wrap">
                        <div class="layui-input-prefix">
                            <i class="layui-icon layui-icon-vercode"></i>
                        </div>
                        <input type="text" name="verCode" value="" lay-verify="required" placeholder="验证码"
                               lay-reqtext="请填写验证码" autocomplete="off" class="layui-input" lay-affix="clear">
                    </div>
                </div>
                <div class="layui-col-xs5">
                    <div style="margin-left: 10px;">
                        <img src="/verCode" onclick="this.src=this.src+'?'+ new Date().getTime();">
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <input type="checkbox" name="remember" value="true" lay-skin="primary" title="记住密码">
<!--            <a href="#forget" style="float: right; margin-top: 7px;">忘记密码？</a>-->
        </div>
        <div class="layui-form-item">
            <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="login">登录</button>
        </div>

    </div>
</form>
<script src="/layui/layui.js"></script>
<script>
    //防止套娃
    (function (window) {
        if (window.location != window.top.location) {
            window.top.location = window.location;
        }
    })(this);

    layui.use(['form'], () => {//function(){ this }
        //箭头函数  匿名函数
        const form = layui.form;//var声明的变量可以重复声明，let不可以重复声明。const常量
        const $ = layui.$;

        form.on('submit(login)', (data) => {
            //ajax
            $.ajax({
                url: '/admin/login',
                data: data.field,
                type: 'post',
                dataType: 'json',
                success(result) {
                    if (result.code == 0) {
                        layer.msg('登录成功！', {icon: 6}, () => {
                            //跳转主页面
                            window.location.href = '/html/index.html';
                        });
                    } else {
                        layer.msg(result.msg, {icon: 5});
                    }
                },
                error() {
                    layer.msg('请求失败！', {icon: 5});
                }
            });
            //阻止表单的自动提交
            return false;
        })
    });
</script>
</body>
</html>